<?php

/**
 * This is the model class for table "job".
 *
 * The followings are the available columns in table 'job':
 * @property string $id
 * @property string $start_date
 * @property string $end_date
 * @property string $name
 * @property string $reward
 * @property string $poster_id
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Application[] $applications
 */
class Job extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Job the static model class
	 */
	public $rewardMax;
        public $start_dateMax;
	 
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'job';
	}

//	public function getRewardMax(){
//             return $this->rewardMax;
//        }

//        public function attributeNames(){
//            $arr = parent::attributeNames();
//            $arr['rewardMax'] = null;
//            return $arr;
//        }

//      public function setRewardMax($RewardMax){
//          $this->_rewardMax = $RewardMax;
//      }

//        public function getAttributes() {
//            $arr['rewardMax'] = null;
//            $arr = parent::getAttributes();
//
//            return $arr;
//        }
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('start_date, end_date, name, reward, poster_id, status', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>100),
			array('reward, rewardMax, poster_id', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, start_date, start_dateMax, end_date, name, reward, rewardMax, poster_id, status', 'safe'),
			
                        array('name, reward, start_date, end_date', 'required', 'message' => '{attribute}: harus terisi'),
                        array('start_date, end_date, start_dateMax', 'date', 'format' => 'yyyy-M-dd H:m:s', 'message' => '{attribute}: format tanggal salah')

		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'applications' => array(self::HAS_MANY, 'Application', 'job_id'),
			'approvedApplication' => array(self::HAS_ONE, 'Application', 'job_id', 'on' => '`approvedApplication`.`status` = 1'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'name' => 'Name',
			'reward' => 'Reward',
			'rewardMax' => 'Reward Max',
                        'start_dateMax' => 'Start Date Max',
			'poster_id' => 'Poster',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('reward',$this->reward,true);
		$criteria->compare('poster_id',$this->poster_id,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	

        /**
         * Get a string represents the reward in user friendly format
         */
        public function getFormattedReward($currency = "Rp ") {
            return $currency . number_format($this->reward);
        }

        /**
         * Get the poster facebook user
         */
        private $fbUser;
        public function getPoster() {
            if($this->fbUser == null) {
                $this->fbUser = new UserFb($this->poster_id);
            }
            return $this->fbUser;
        }

        public function isThePosterIsMe() {
            return $this->getPoster()->getId() == Yii::app()->params['userFb']->getId();
        }
        
        public function canApply() {
            //if this job is not active, you can't apply
            if($this->status != ConstantsLookUp::$JOBSTATUS_ACTIVE) return false;

            //check whether application with follwing criteria exists
            //  the user_id is current user, the job_id is this job's id, and status either waiting or approved
            $curUser    = Yii::app()->params['userFb']->getId();
            if($this->poster_id == $curUser) return false;
            $rejected   = ConstantsLookUp::$APPSTATUS_REJECTED;
            return Application::model()->find("`job_id` = $this->id AND `user_id` = $curUser AND `status` <> $rejected") == null;
        }

        public function isApplicationActionable($applicationModel) {
            //if this status is not waiting or job is not active, return false
            return ($this->poster_id == Yii::app()->params['userFb']->getId() && $applicationModel->status == ConstantsLookUp::$APPSTATUS_WAITING && $this->status == ConstantsLookUp::$JOBSTATUS_ACTIVE);
        }
}